﻿
#region Usings

using PolyGameEngine.GamePieces;
using PolyGameEngine.GamePieces.Common;
using PolyGameEngine.Tools;

using FarseerPhysics.Dynamics;
using FarseerPhysics.Dynamics.Joints;
using FarseerPhysics.Factories;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

#endregion


namespace PolyMorph.CustomGamePieces
{
    public class RoundRobin : TextureGamePiece
    {
        FixedRevoluteJoint joint;
        float degreesPerUpdate
        {
            get { return revolutionsPerMinute / 10; }
        }
        float revolutionsPerMinute;

        public RoundRobin(PhysicsGame PhysicsGame, World World, Material Material, Texture2D RobinTexture, Vector2 Position, float RevolutionsPerMinute, float Inertia, float Rotation, int saveLineIndex, string GPID)
            : base(PhysicsGame, World, Material, RobinTexture, Position,Rotation , 1, saveLineIndex, GPID)
        {
            revolutionsPerMinute = RevolutionsPerMinute;
            bodyType = BodyType.Dynamic;
            joint = JointFactory.CreateFixedRevoluteJoint(world, body, Vector2.Zero, ConvertToSim(position));
            body.Inertia = Inertia;
            body.CollisionCategories = Category.Cat10;
            body.CollidesWith = Category.Cat1;
        }

        

        float totalRotation = 0;
        float RotationChange = 0;
        public override void Update(GameTime gameTime)
        {
            if (degreesPerUpdate != 0)
            {
                RotationChange = rotation - totalRotation;
                totalRotation = rotation;
                if (RotationChange < MathHelper.ToRadians(degreesPerUpdate))
                    body.ApplyTorque(100);
                else
                    body.ApplyTorque(-100);
            }
            base.Update(gameTime);
        }

    }
}
